package com.risetechnology.krystalkitchen1;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.risetechnology.adapter.ShoppingListAdapter;
import com.risetechnology.common.Shared;
import com.risetechnology.data.Shopping;
import com.risetechnology.master.NavigationActivity;

public class ActivityShoppingList extends NavigationActivity {

	private ListView listviewShopping;
	public static List<Shopping> shoppingList;
	private static ShoppingListAdapter listAdapter;
	
	private LinearLayout layoutCreateNewList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_shopping_list);

		setContents();
	}

	private void setContents() {
		layoutCreateNewList = (LinearLayout) findViewById(R.id.layout_create_new_list);
		layoutCreateNewList.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showCreateNewShoppingListDialog();
			}
		});
		
		listviewShopping = (ListView) findViewById(R.id.lv_shopping_list);
		shoppingList = new ArrayList<Shopping>();
		listAdapter = new ShoppingListAdapter(ActivityShoppingList.this, R.layout.list_item_shopping, shoppingList);
		listviewShopping.setAdapter(listAdapter);
		
		getShoppingList();
	}
	
	public static void getShoppingList() {
		if (ActivityShoppingList.shoppingList != null) {
			ActivityShoppingList.shoppingList.clear();
		}
		
		Shared.dbAdapter.getShoppingList(false);
		
		listAdapter.notifyDataSetChanged();
	}
	
	private void showCreateNewShoppingListDialog() {
		final Dialog dialog = new Dialog(ActivityShoppingList.this.getParent());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_create_new_shopping_list);

		Window window = dialog.getWindow();
		WindowManager.LayoutParams wlp = window.getAttributes();

		wlp.gravity = Gravity.CENTER;
		wlp.width = WindowManager.LayoutParams.FILL_PARENT;
		wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
		window.setAttributes(wlp);

		dialog.getWindow().setAttributes(wlp);

		dialog.show();

		final EditText edittextName = (EditText) dialog.findViewById(R.id.et_shopping_list_name);

		Button buttonOK = (Button) dialog.findViewById(R.id.btn_ok);
		buttonOK.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String shoppingName = edittextName.getText().toString();
				if(shoppingName.trim().length()==0){
					Toast.makeText(ActivityShoppingList.this.getParent(), "Please input name", Toast.LENGTH_SHORT).show();
					return;
				}
				
				dialog.dismiss();

				
				
				
				Shopping shopping = new Shopping("", shoppingName);
				Shared.dbAdapter.addShoppingList(shopping);

				Toast.makeText(ActivityShoppingList.this.getParent(), shoppingName + " created!", Toast.LENGTH_LONG).show();

				getShoppingList();
			}
		});
	}
	

}
